package com.fact.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.FactEntryParamDTO;
import com.fact.dto.NodeFactDTO;
import com.fact.dto.NodeHierarchyDTO;
import com.fact.dto.ScenarioDTO;
import com.fact.models.FactService;
import com.fact.models.ScenarioService;
import com.fact.models.UserService;
import com.fact.utils.AppConstant;

public class FactEntry extends BaseAction implements SessionAware{

	private static final long serialVersionUID = 1L;
	
	private Map<String,Object> session;
	
	private String page=AppConstant.FACT_ENTRY;
	private String pageTitle="FMtool - Fact Entry";
	private List<ScenarioDTO> scenarioList;
	private ArrayList<NodeHierarchyDTO> nodeHierarchy;
	private FactEntryParamDTO factEntryParam;
	FactService  factService=new FactService();
	ScenarioService scenarioService=new ScenarioService();
	private int selectedMonth;
	private int selectedYear;
	private NodeFactDTO kpi;
	
	
	public String factEntryForm()
	{
		
		if(factEntryParam.getSelectedMonth()!=0) 
			selectedMonth=factEntryParam.getSelectedMonth();
		else
			selectedMonth=Integer.parseInt(getServletContext().getAttribute("CURRENT_MONTH").toString());
		
		if(factEntryParam.getSelectedYear()!=0)
			selectedYear=factEntryParam.getSelectedYear();
		else
			selectedYear=Integer.parseInt(getServletContext().getAttribute("CURRENT_YEAR").toString());
		
		nodeHierarchy=factService.getNodeHierarchy(factEntryParam.getSegmentId(), factEntryParam.getMakeId(), factEntryParam.getMarketId(), selectedMonth, selectedYear);
		scenarioList=scenarioService.getScenarioList();
		return SUCCESS;
	}
	
	public String saveKPI()
	{

		HttpServletResponse response = ServletActionContext.getResponse();
		boolean res=factService.saveKPIvalue(kpi);	
		String responseString="<font color='green'>Not Saved</font>";
		if(res==true)
			responseString="<font color='green'>Saved</font>";
		try{
        	response.setContentType("text/xml");
        	response.setHeader("Cache-Control", "no-cache");
        	response.getWriter().write(responseString);
        	response.flushBuffer();
          }
        catch(Exception e) {e.printStackTrace();}
        
		return null;
	}
	
	
	public String getPage() {
		return page;
	}


	public void setPage(String page) {
		this.page = page;
	}


	public String getPageTitle() {
		return pageTitle;
	}


	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}
	public List<ScenarioDTO> getScenarioList() {
		return scenarioList;
	}
	public void setScenarioList(List<ScenarioDTO> scenarioList) {
		this.scenarioList = scenarioList;
	}



	public ArrayList<NodeHierarchyDTO> getNodeHierarchy() {
		return nodeHierarchy;
	}


	public void setNodeHierarchy(ArrayList<NodeHierarchyDTO> nodeHierarchy) {
		this.nodeHierarchy = nodeHierarchy;
	}


	public FactEntryParamDTO getFactEntryParam() {
		return factEntryParam;
	}


	public void setFactEntryParam(FactEntryParamDTO factEntryParam) {
		this.factEntryParam = factEntryParam;
	}


	public int getSelectedMonth() {
		return selectedMonth;
	}


	public void setSelectedMonth(int selectedMonth) {
		this.selectedMonth = selectedMonth;
	}


	public int getSelectedYear() {
		return selectedYear;
	}


	public void setSelectedYear(int selectedYear) {
		this.selectedYear = selectedYear;
	}


	public NodeFactDTO getKpi() {
		return kpi;
	}


	public void setKpi(NodeFactDTO kpi) {
		this.kpi = kpi;
	}


	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}
}
